<template>
    <div class="modal fade" id="adviserModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true" style="z-index: 1000">
        <div class="modal-dialog" style="width: 1000px;">
            <div class="modal-content" style="text-align: center;width: 1000px;">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal">
                        <span aria-hidden="true">&times;</span><span class="sr-only">Close</span>
                    </button>
                    <h4 class="modal-title" id="myModalLabel">{{referrerTitle}}{{ originalAdviser }}</h4>
                </div>
                <div class="modal-body" >
                    <adviser-setting :premises-id="premisesId" ref="adviserSetting"
                                      :adviser-list="adviserList"
                                      :loading="getAdviserLoading"
                                      v-on:outerAdviserListener='outerAdviserListener'
                    ></adviser-setting>
                </div>
                <div class="modal-footer" v-show="showSaveBtn" >
                    <button type="button" class="btn checkedBtn" @click="saveAdviser" v-res="{code:'menu_0004_0001_0007_0002,menu_0004_0001_0007_0004'}">保存设置</button>
                </div>
            </div>
        </div>
        <addVisitReview :adviser-list="adviserList" v-on:outerAdviserListener='outerAdviserListener'></addVisitReview>
        <adviserChangeModal :premises-id="premisesId" :original-adviser="originalAdviser" :original-adviser-list="originalAdviserList"></adviserChangeModal>
    </div>
</template>

<script>
import { Modal } from 'iview'
import {API, Log, Utils, Service} from '../../../js/base'
import AdviserSetting from './AdviserSetting.vue'
import addVisitReview from '../premiseVisitReview.vue'
import adviserChangeModal from './AdviserChangeModal.vue'
export default {
  name: 'AdviserModal',
  props: {
    referrerTitle: {
      type: String,
      default: ''
    },
    premisesId: {
      type: String,
      default: null
    }
  },
  components: {
    Modal,
    AdviserSetting,
    addVisitReview,
    adviserChangeModal
  },
  watch: {
    premisesId (val) {
      if (!val) return
      this.getAdviserList(val)
      this.premisesId = val;
    }
  },
  data () {
    return {
      getAdviserLoading: false,
      adviserList: [],
      originalAdviserList: [],
      originalAdviser: '',
      premisesId: '',
      showSaveBtn: true
    }
  },
  mounted () {
  },
  methods: {
    resetData () {
      this.$emit("setAdviserListener");
      $('#adviserModal').modal('hide')
    },
    getAdviserList (id = this.premisesId) {
      const _this = this
      _this.getAdviserLoading = true
      _this.adviserList = []
      _this.originalAdviserList = []
      //const tmpList = []
      if(!id) return
      let index = -1
      Service.Adviser.getAdviserList(id, this).then(list => {
        _this.getAdviserLoading = false
        if (list && list.length) {
          list.forEach(item => {
            item.name = item.name
            item.phone = item.phone
            // 添加此字段,是为了人员选择组件中的选中效果
            item.recommenderId = item.partnerId;
            index = _this.adviserList.findIndex(i => i.phone === item.phone)
            if(index < 0) { // 未找到
              _this.adviserList.push(item)
              _this.originalAdviserList.push(item);
            }
          })
        }
      }).catch(e => {
        _this.getAdviserLoading = false
      })
    },
    saveAdviser () {
      const _this = this
      _this.showSaveBtn = false;
      let adviserList = []
      this.$refs.adviserSetting.tableData.forEach(item => {
        let obj = {};
        obj.partnerId = item.partnerId;
        obj.name = item.name;
        obj.phone = item.phone;
        adviserList.push(obj)
      })
      // 保存推荐人
      const p1 = Service.Adviser.saveAdviser({
        premisesId: _this.premisesId,
        adviserList,
        _this
      }).then(rsp => {
        if(rsp.result === "1"){
          _this.resetData()
        }
        _this.showSaveBtn = true;
        return rsp
      }).catch(e => {
        _this.showSaveBtn = true;
        return e
      })
    },
    outerAdviserListener(outerData){
      this.adviserList = outerData;
    }
  }
}
</script>

<style scoped>
</style>
